package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.TiJianAdapter;
import com.tangkun.environment.entity.shouye.ErrorListReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 体检 列表
 */

public class TiJianActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;
    /**
     * 0基础数据 1巡查数据
     */
    private String type = "basic";

    public int page = 1;// 当前页

    /**
     * 数据源
     */
    private List<ErrorListReturn.DataBean> dataList = new ArrayList<ErrorListReturn.DataBean>();
    /**
     * 适配器
     */
    private TiJianAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        type = getIntent().getStringExtra("type");
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
        title.setText("体检");
        errorList();
    }

    private void initView() {
        adapter = new TiJianAdapter(TiJianActivity.this, dataList);
        switch (type) {
            case "basic"://基础数据
                adapter.setType(0);
                break;
            case "inspect"://巡查数据
                adapter.setType(1);
                break;
        }
        customListView.setAdapter(adapter);
    }

    @OnClick(R.id.back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
        }
    }


    /**
     * 体检
     */
    private void errorList() {
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        map.put("pageNumber", page + "");
        map.put("pageSize", pageSize + "");
        fromNetwork.errorList(map).compose(new RxActivityHelper<ErrorListReturn>().ioMain(TiJianActivity.this, true))
                .subscribe(new MySubscribe<ErrorListReturn>(mContext, true) {
                    @Override
                    protected void success(ErrorListReturn resultBean) {
                        if (resultBean.data != null) {
                            if (resultBean.data.size() == 0) {
                                customListView.setVisibility(View.GONE);
                            } else {
                                customListView.setVisibility(View.VISIBLE);
                            }
                            dataList.clear();
                            //填充数据
                            dataList.addAll(resultBean.data);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}
